package vip.xiaonuo.sys.fymodule.thirdpa.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.sys.config.PayConfig;
import vip.xiaonuo.sys.config.TaskExecutePoolConfig;
import vip.xiaonuo.sys.core.util.PaPayUtil;
import vip.xiaonuo.sys.fymodule.account.entity.SxoSafetyCardBinding;
import vip.xiaonuo.sys.fymodule.account.entity.SxoSafetyUser;
import vip.xiaonuo.sys.fymodule.account.enums.CodeEnum;
import vip.xiaonuo.sys.fymodule.account.service.ISxoSafetyCardBindingService;
import vip.xiaonuo.sys.fymodule.account.service.ISxoSafetyLogService;
import vip.xiaonuo.sys.fymodule.account.service.ISxoSafetyUserService;
import vip.xiaonuo.sys.fymodule.account.util.HttpResult;
import vip.xiaonuo.sys.fymodule.account.util.TradetraceUtil;
import vip.xiaonuo.sys.fymodule.exception.BusinessException;
import vip.xiaonuo.sys.fymodule.order.entity.*;
import vip.xiaonuo.sys.fymodule.order.service.*;
import vip.xiaonuo.sys.fymodule.thirdpa.api.IBaseClient;
import vip.xiaonuo.sys.fymodule.thirdpa.api.impl.*;
import vip.xiaonuo.sys.fymodule.thirdpa.model.dto.*;
import vip.xiaonuo.sys.fymodule.thirdpa.model.vo.*;
import vip.xiaonuo.sys.fymodule.thirdpa.service.PaBondService;
import vip.xiaonuo.sys.fymodule.thirdpa.service.PaMemberService;

import java.util.List;

@Slf4j
@Service
public class PaBondServiceImpl extends BasePaServiceImpl implements PaBondService {


    @Autowired
    private ISxoSafetyLogService sxoSafetyLogService;
    @Autowired
    private PayConfig payConfig;
    @Autowired
    private TaskExecutePoolConfig taskExecutePoolConfig;
    @Autowired
    private ISxoSafetyOrderService sxoSafetyOrderService;
    @Autowired
    private ISxoSafetyOrderDetailService sxoSafetyOrderDetailService;
    @Autowired
    private ISxoSafetyFreezeFundsService sxoSafetyFreezeFundsService;
    @Autowired
    private ISxoSafetyMemTranService sxoSafetyMemTranService;
    @Autowired
    private ISxoSafetyMemWithdrawalService sxoSafetyMemWithdrawalService;
    @Autowired
    private ISxoSafetyUserService sxoSafetyUserService;
    @Autowired
    private ISxoSafetyCardBindingService sxoSafetyCardBindingService;
    @Autowired
    private PaMemberService paMemberService;
    @Autowired
    private ISxoSafetyMemTranRefundService sxoSafetyMemTranRefundService;

    @Override
    public MembershipTrancheFreezeVo membershipTrancheFreeze(MembershipTrancheFreezeDto dto) {
        if (StrUtil.isEmpty(dto.getId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id必须填写");
        }
        SxoSafetyOrder safetyOrder = sxoSafetyOrderService.getById(dto.getId());
        if (safetyOrder == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id错误或者已撤退挂账");
        }


        LambdaQueryWrapper<SxoSafetyOrderDetail> qw = new LambdaQueryWrapper<>();
        qw.eq(SxoSafetyOrderDetail::getSafetyOrderId, safetyOrder.getId());
        List<SxoSafetyOrderDetail> orderDetails = sxoSafetyOrderDetailService.list(qw);
        if (orderDetails.isEmpty()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "冻结充值明细错误");
        }

        SxoSafetyOrderDetail orderDetail = orderDetails.get(0);

        String orderNo = TradetraceUtil.buildTradetraceAccTranche();
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6007");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        //订单信息
        json.put("FunctionFlag", dto.getFunctionFlag());
        json.put("SubAcctNo", orderDetail.getSubOrderFillSubAcctNo());
        json.put("TranNetMemberCode", safetyOrder.getTranNetMemberCode());
        json.put("ConsumeAmt", dto.getConsumeAmt());
        json.put("TranAmt", dto.getTranAmt());
        json.put("TranCommission", dto.getTranCommission());

        json.put("Ccy", "RMB");
        json.put("OrderNo", orderNo);
        json.put("OrderContent", "");

        SxoSafetyFreezeFunds sxoSafetyFreezeFunds = new SxoSafetyFreezeFunds();
        sxoSafetyFreezeFunds.setUserId(safetyOrder.getUserId());
        sxoSafetyFreezeFunds.setSafetyOrderId(safetyOrder.getId());
        sxoSafetyFreezeFunds.setOrderId(safetyOrder.getOrderId());
        sxoSafetyFreezeFunds.setOrderNo(safetyOrder.getOrderNo());
        sxoSafetyFreezeFunds.setShopId(safetyOrder.getShopId());
        sxoSafetyFreezeFunds.setFunctionFlag(dto.getFunctionFlag());
        sxoSafetyFreezeFunds.setSubAcctNo(orderDetail.getSubOrderFillSubAcctNo());
        sxoSafetyFreezeFunds.setTrannetMemberCode(safetyOrder.getTranNetMemberCode());
        sxoSafetyFreezeFunds.setConsumeAmt(dto.getConsumeAmt());
        sxoSafetyFreezeFunds.setTranAmt(dto.getTranAmt());
        sxoSafetyFreezeFunds.setTranCommission(dto.getTranCommission());
        sxoSafetyFreezeFunds.setCcy("RMB");
        sxoSafetyFreezeFunds.setFreezeOrderNo(orderNo);
        sxoSafetyFreezeFunds.setStatus(0);
        sxoSafetyFreezeFunds.setAddTime(DateUtil.now());
        sxoSafetyFreezeFunds.setUpdTime(DateUtil.now());
        sxoSafetyFreezeFundsService.save(sxoSafetyFreezeFunds);
        try {
            IBaseClient baseClient = new MembershipTrancheFreezeApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            String jzbAcceptSeqNo = extenddata.getString("FrontSeqNo");

            String date = DateUtil.now();
            SxoSafetyFreezeFunds mod = new SxoSafetyFreezeFunds();
            mod.setId(sxoSafetyFreezeFunds.getId());
            mod.setFreezeStatus(0);
            mod.setFreezeTime(date);
            mod.setJzbAcceptSeqNo(jzbAcceptSeqNo);
            mod.setCnsmrSeqNo(extenddata.getString(PaPayUtil.CNSMR_SEQ_NO));
            sxoSafetyFreezeFundsService.updateById(mod);

            MembershipTrancheFreezeVo vo = new MembershipTrancheFreezeVo();
            vo.setFrontSeqNo(jzbAcceptSeqNo);
            return vo;
        } catch (Exception e) {
            SxoSafetyFreezeFunds mod = new SxoSafetyFreezeFunds();
            mod.setId(sxoSafetyFreezeFunds.getId());
            mod.setUpdTime(DateUtil.now());
            mod.setFreezeStatus(-1);
            mod.setErrorMsg(e.getMessage());
            sxoSafetyFreezeFundsService.updateById(mod);
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public MemberTransactionVo memberTransaction(MemberTransactionDto dto) {
        if (StrUtil.isEmpty(dto.getId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id必须填写");
        }
        SxoSafetyFreezeFunds freezeFunds = sxoSafetyFreezeFundsService.getById(dto.getId());
        if (freezeFunds == null) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "id错误或者已撤退挂账");
        }


        String orderNo = TradetraceUtil.buildTradetraceMemTran();
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6034");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        //订单信息
        json.put("FunctionFlag", dto.getFunctionFlag());
        json.put("OutSubAcctNo", dto.getOutSubAcctNo());
        json.put("OutMemberCode", dto.getOutMemberCode());
        json.put("OutSubAcctName", dto.getOutSubAcctName());
        json.put("InSubAcctNo", dto.getInSubAcctNo());
        json.put("InMemberCode", dto.getInMemberCode());
        json.put("InSubAcctName", dto.getInSubAcctName());

        json.put("TranAmt", dto.getTranAmt());
        json.put("TranFee", dto.getTranFee());
        json.put("TranType", "01");

        json.put("Ccy", "RMB");
        json.put("OrderNo", orderNo);
        json.put("OrderContent", "");
        json.put("Remark", "");

        SxoSafetyMemTran memTran = new SxoSafetyMemTran();
        memTran.setUserId(freezeFunds.getUserId());
        memTran.setSafetyOrderId(freezeFunds.getId());
        memTran.setOrderId(freezeFunds.getOrderId());
        memTran.setOrderNo(freezeFunds.getOrderNo());
        memTran.setShopId(freezeFunds.getShopId());
        memTran.setFunctionFlag(dto.getFunctionFlag());
        memTran.setOutSubAcctNo(dto.getOutSubAcctNo());
        memTran.setOutMemberCode(dto.getOutMemberCode());
        memTran.setOutSubAcctName(dto.getOutSubAcctName());
        memTran.setInSubAcctNo(dto.getInSubAcctNo());
        memTran.setInMemberCode(dto.getInMemberCode());
        memTran.setInSubAcctName(dto.getInSubAcctName());
        memTran.setTranAmt(dto.getTranAmt());
        memTran.setTranFee(dto.getTranFee());
        memTran.setCcy("RMB");
        memTran.setTranType("01");
        memTran.setTranOrderNo(orderNo);
        memTran.setStatus(0);
        memTran.setAddTime(DateUtil.now());
        memTran.setUpdTime(DateUtil.now());
        sxoSafetyMemTranService.save(memTran);
        try {
            IBaseClient baseClient = new MemberTransactionApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            String jzbAcceptSeqNo = extenddata.getString("FrontSeqNo");

            String date = DateUtil.now();
            SxoSafetyMemTran mod = new SxoSafetyMemTran();
            mod.setId(memTran.getId());
            mod.setTranStatus(0);
            mod.setTranTime(date);
            mod.setCnsmrSeqNo(extenddata.getString("CnsmrSeqNo"));
            mod.setJzbAcceptSeqNo(jzbAcceptSeqNo);
            sxoSafetyMemTranService.updateById(mod);

            MemberTransactionVo vo = new MemberTransactionVo();
            vo.setFrontSeqNo(jzbAcceptSeqNo);
            return vo;
        } catch (Exception e) {
            SxoSafetyMemTran mod = new SxoSafetyMemTran();
            mod.setId(memTran.getId());
            mod.setUpdTime(DateUtil.now());
            mod.setTranStatus(-1);
            mod.setErrorMsg(e.getMessage());
            sxoSafetyMemTranService.updateById(mod);
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public ApplicationTextMsgDynamicCodeVo applicationTextMsgDynamicCode(ApplicationTextMsgDynamicCodeDto dto) {
        if (StrUtil.isEmpty(dto.getSubAcctNo())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "见证子账户的账号必须填写");
        }

        String orderNo = TradetraceUtil.buildTradetraceDynamicCode();
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6082");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        //订单信息
        json.put("TranNetMemberCode", dto.getTranNetMemberCode());
        json.put("SubAcctNo", dto.getSubAcctNo());
        json.put("TranType", dto.getTranType());
        json.put("TranAmt", dto.getTranAmt());
        json.put("BankCardNo", dto.getBankCardNo());
        json.put("OrderNo", orderNo);
        json.put("Remark", "");

        try {
            IBaseClient baseClient = new ApplicationTextMsgDynamicCodeApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            JSONObject object = buildResult(extenddata);
            ApplicationTextMsgDynamicCodeVo vo = JSONObject.parseObject(object.toJSONString(), ApplicationTextMsgDynamicCodeVo.class);
            vo.setOrderNo(orderNo);
            return vo;
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public MemberWithdrawCashVo memberWithdrawCash(MemberWithdrawCashDto dto) {
        if (StrUtil.isEmpty(dto.getSubAcctNo())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "见证子账户的账号必须填写");
        }

        LambdaQueryWrapper<SxoSafetyUser> wq = new LambdaQueryWrapper<>();
        wq.eq(SxoSafetyUser::getSubAcctNo, dto.getSubAcctNo());
        wq.eq(SxoSafetyUser::getStatus, "0");
        wq.eq(SxoSafetyUser::getReinStatus, "1");
        List<SxoSafetyUser> users = sxoSafetyUserService.list(wq);
        if (users.isEmpty()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "见证子账户的账号错误");
        }
        SxoSafetyUser user = users.get(0);

        LambdaQueryWrapper<SxoSafetyCardBinding> wqBind = new LambdaQueryWrapper<>();
        wqBind.eq(SxoSafetyCardBinding::getUserId, user.getUserId());
        wqBind.eq(SxoSafetyCardBinding::getStatus, "0");
        wqBind.eq(SxoSafetyCardBinding::getBindFlag, "1");
        wqBind.eq(SxoSafetyCardBinding::getSource, user.getSource());
        List<SxoSafetyCardBinding> bindings = sxoSafetyCardBindingService.list(wqBind);
        if (bindings.isEmpty()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "当前用户未绑定银行卡");
        }
        SxoSafetyCardBinding binding = bindings.get(0);

        String subAcctName = user.getSubAcctName();
        if (StrUtil.isEmpty(subAcctName)) {
            EJZBCustInformationQueryDto queryDto = new EJZBCustInformationQueryDto();
            queryDto.setCustAcctId(user.getSubAcctNo());
            queryDto.setThirdCustId(user.getTranNetMemberCode());
            Object object = paMemberService.eJZBCustInformationQuery(queryDto);
            if (object == null) {
                throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "见证子账户的账号在平安系统错误");
            }
            JSONObject jsonObject = (JSONObject) object;
            subAcctName = jsonObject.getString("custName");
            if (StrUtil.isNotEmpty(subAcctName)) {
                SxoSafetyUser uMod = new SxoSafetyUser();
                uMod.setId(user.getId());
                uMod.setSubAcctName(subAcctName);
                sxoSafetyUserService.updateById(uMod);
            }
        }
        if (StrUtil.isEmpty(subAcctName)) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "见证子账户的户名平安系统错误");
        }
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6085");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        //订单信息
        json.put("SubAcctNo", user.getSubAcctNo());
        json.put("TranNetMemberCode", user.getTranNetMemberCode());
        json.put("SubAcctName", subAcctName);
        json.put("TakeCashAcctNo", binding.getMemberAcctNo());
        json.put("TakeCashAcctName", binding.getMemberName());
        json.put("Ccy", "RMB");
        json.put("CashAmt", dto.getTranAmt());
        json.put("TakeCashCommission", "0");
        json.put("MessageOrderNo", dto.getMessageOrderNo());
        json.put("MessageCheckCode", dto.getMessageCheckCode());
        json.put("Remark", "");
        json.put("WebSign", "");

        SxoSafetyMemWithdrawal withdrawal = new SxoSafetyMemWithdrawal();
        withdrawal.setUserId(user.getUserId());
        withdrawal.setSubAcctNo(user.getSubAcctNo());
        withdrawal.setSubAcctName(subAcctName);
        withdrawal.setTrannetMemberCode(user.getTranNetMemberCode());
        withdrawal.setTakeCashAcctNo(binding.getMemberAcctNo());
        withdrawal.setTakeCashAcctName(binding.getMemberName());
        withdrawal.setCcy("RMB");
        withdrawal.setCashAmt(dto.getTranAmt());
        withdrawal.setTakeCashCommission(json.getString("TakeCashCommission"));
        withdrawal.setTranType(dto.getTranType());
        withdrawal.setCashOrderNo(dto.getCashOrderNo());
        withdrawal.setCashStatus(0);
        withdrawal.setStatus(0);
        withdrawal.setAddTime(DateUtil.now());
        withdrawal.setUpdTime(DateUtil.now());
        try {

            sxoSafetyMemWithdrawalService.save(withdrawal);

            IBaseClient baseClient = new MemberWithdrawCashApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            String jzbAcceptSeqNo = extenddata.getString("FrontSeqNo");

            String date = DateUtil.now();
            SxoSafetyMemWithdrawal mod = new SxoSafetyMemWithdrawal();
            mod.setId(withdrawal.getId());
            mod.setCashStatus(0);
            mod.setCashTime(date);
            mod.setJzbAcceptSeqNo(jzbAcceptSeqNo);
            mod.setCnsmrSeqNo(extenddata.getString(PaPayUtil.CNSMR_SEQ_NO));
            sxoSafetyMemWithdrawalService.updateById(mod);
            MemberWithdrawCashVo vo = new MemberWithdrawCashVo();
            vo.setFrontSeqNo(jzbAcceptSeqNo);
            return vo;
        } catch (Exception e) {
            SxoSafetyMemWithdrawal mod = new SxoSafetyMemWithdrawal();
            mod.setId(withdrawal.getId());
            mod.setUpdTime(DateUtil.now());
            mod.setCashStatus(-1);
            mod.setErrorMsg(e.getMessage());
            sxoSafetyMemWithdrawalService.updateById(mod);
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public MemberTranVerifyTextMsgsVo memberTranVerifyTextMsgs(MemberTranVerifyTextMsgsDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6101");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        //订单信息
        json.put("OutSubAcctNo", dto.getOutSubAcctNo());
        json.put("OutMemberCode", dto.getOutMemberCode());
        json.put("OutSubAcctName", dto.getOutMemberName());
        json.put("InSubAcctNo", dto.getInSubAcctNo());
        json.put("InMemberCode", dto.getInMemberCode());
        json.put("InSubAcctName", dto.getInMemberName());
        json.put("TranAmt", dto.getTranAmt());
        json.put("TranFee", dto.getTranFee());
        json.put("TranType", "01");
        json.put("Ccy", "RMB");
        json.put("OrderNo", "");
        json.put("Remark", "");
        json.put("MessageOrderNo", dto.getMessageOrderNo());
        json.put("MessageCheckCode", dto.getMessageCheckCode());
        try {


            IBaseClient baseClient = new MemberTranVerifyTextMsgsApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            String jzbAcceptSeqNo = extenddata.getString("FrontSeqNo");
            MemberTranVerifyTextMsgsVo vo = new MemberTranVerifyTextMsgsVo();
            vo.setFrontSeqNo(jzbAcceptSeqNo);
            return vo;
        } catch (Exception e) {

            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public BankWithdrawCashBackQueryVo bankWithdrawCashBackQuery(BankWithdrawCashBackQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6048");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        //订单信息
        json.put("FunctionFlag", dto.getFunctionFlag());
        json.put("SubAcctNo", dto.getSubAcctNo());
        json.put("StartDate", dto.getStartDate());
        json.put("EndDate", dto.getEndDate());

        try {
            IBaseClient baseClient = new BankWithdrawCashBackQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), BankWithdrawCashBackQueryVo.class);
        } catch (Exception e) {

            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public QueryCustAcctIdVo queryCustAcctId(QueryCustAcctIdDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6037");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        //订单信息
        json.put("TranNetMemberCode", dto.getTranNetMemberCode());

        try {
            IBaseClient baseClient = new QueryCustAcctIdApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), QueryCustAcctIdVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public Object apntTransfer(ApntTransferDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6211");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        //订单信息
        json.put("RecvAcctNo", dto.getRecvAcctNo());
        json.put("RecvAcctName", dto.getRecvAcctName());
        json.put("RecvAcctOpenBranchName", dto.getRecvAcctOpenBranchName());
        json.put("RecvAcctOpenBranchInterbankId", dto.getRecvAcctOpenBranchInterbankId());
        json.put("ApplyTakeCashAmt", dto.getApplyTakeCashAmt());
        json.put("MarketChargeCommission", dto.getMarketChargeCommission());
        json.put("Remark", dto.getRemark());

        try {
            IBaseClient baseClient = new ApntTransferApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return buildResult(extenddata);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public SupAcctIdBalanceQueryVo supAcctIdBalanceQuery() {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6011");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());

        try {
            IBaseClient baseClient = new SupAcctIdBalanceQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), SupAcctIdBalanceQueryVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public MemberTransactionRefundVo memberTransactionRefund(MemberTransactionRefundDto dto) {
        if (StrUtil.isEmpty(dto.getSafetyTranId())) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), "会员交易ID必须填写");
        }
        SxoSafetyMemTran memTran = sxoSafetyMemTranService.getById(dto.getSafetyTranId());

        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6164");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        //订单信息
        json.put("FunctionFlag", StrUtil.isEmpty(dto.getFunctionFlag()) ? "2" : dto.getFunctionFlag());
        json.put("OldTranSeqNo", memTran.getCnsmrSeqNo());
        json.put("OldOutSubAcctNo", memTran.getOutSubAcctNo());
        json.put("OldOutMemberCode", memTran.getOutMemberCode());
        json.put("OldInSubAcctNo", memTran.getInSubAcctNo());
        json.put("OldInMemberCode", memTran.getInMemberCode());
        json.put("OldOrderNo", "");
        json.put("ReturnAmt", memTran.getTranAmt());
        json.put("ReturnCommission", memTran.getTranFee());
        json.put("Remark", "");
        json.put("ReservedMsg", "");

        SxoSafetyMemTranRefund refund = new SxoSafetyMemTranRefund();
        refund.setFunctionFlag(Integer.parseInt(json.getString("FunctionFlag")));
        refund.setUserId(memTran.getUserId());
        refund.setSafetyTranId(memTran.getId());
        refund.setOrderId(memTran.getOrderId());
        refund.setOrderNo(memTran.getOrderNo());
        refund.setShopId(memTran.getShopId());
        refund.setOldTranSeqNo(memTran.getCnsmrSeqNo());
        refund.setOldOutSubAcctNo(memTran.getOutSubAcctNo());
        refund.setOldOutMemberCode(memTran.getOutMemberCode());
        refund.setOldInSubAcctNo(memTran.getInSubAcctNo());
        refund.setOldInMemberCode(memTran.getInMemberCode());
        refund.setOldOrderNo(json.getString("OldOrderNo"));
        refund.setRefundAmt(memTran.getTranAmt());
        refund.setRefundFee(memTran.getTranFee());
        refund.setRemark(json.getString("Remark"));
        refund.setRefundStatus(2);
        refund.setStatus(0);
        refund.setAddTime(DateUtil.now());
        refund.setUpdTime(DateUtil.now());
        sxoSafetyMemTranRefundService.save(refund);
        try {

            IBaseClient baseClient = new MemberTransactionRefundApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            String jzbAcceptSeqNo = extenddata.getString("FrontSeqNo");
            SxoSafetyMemTranRefund mod = new SxoSafetyMemTranRefund();
            mod.setId(refund.getId());
            mod.setRefundTime(DateUtil.now());
            mod.setUpdTime(DateUtil.now());
            mod.setJzbAcceptSeqNo(jzbAcceptSeqNo);
            mod.setCnsmrSeqNo(extenddata.getString(PaPayUtil.CNSMR_SEQ_NO));
            sxoSafetyMemTranRefundService.updateById(mod);

            MemberTransactionRefundVo vo = new MemberTransactionRefundVo();
            vo.setFrontSeqNo(jzbAcceptSeqNo);
            return vo;
        } catch (Exception e) {
            SxoSafetyMemTranRefund mod = new SxoSafetyMemTranRefund();
            mod.setId(refund.getId());
            mod.setRefundStatus(-2);
            mod.setErrorMsg(e.getMessage());
            mod.setUpdTime(DateUtil.now());
            sxoSafetyMemTranRefundService.updateById(mod);
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public ReconciliationDocumentQueryVo reconciliationDocumentQuery(ReconciliationDocumentQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6103");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("FileType", dto.getFileType());
        json.put("FileDate", dto.getFileDate());

        try {
            IBaseClient baseClient = new ReconciliationDocumentQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject obj = JSON.parseObject(httpResult.getBody());
            JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

            ReconciliationDocumentQueryVo vo = new ReconciliationDocumentQueryVo();
            if (extenddata.get(PaPayUtil.code) != null && !extenddata.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
                vo.setTxnReturnMsg(extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
                return vo;
            }
            if (extenddata.get(PaPayUtil.TXN_RETURN_CODE) != null && !extenddata.getString(PaPayUtil.TXN_RETURN_CODE).equals(PaPayUtil.code_000000)) {
                vo.setTxnReturnMsg(extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
                return vo;
            }
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), ReconciliationDocumentQueryVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public BankTransactionDetailsQueryVo bankTransactionDetailsQuery(BankTransactionDetailsQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6072");
        json.put("FunctionFlag", dto.getFunctionFlag());
        json.put("FundSummaryAcctNo", dto.getFundSummaryAcctNo());
        json.put("SubAcctNo", dto.getSubAcctNo());
        json.put("QueryFlag", dto.getSubAcctNo());
        json.put("PageNum", dto.getPageNum());
        try {
            IBaseClient baseClient = new BankTransactionDetailsQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), BankTransactionDetailsQueryVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public BankWithdrawCashDetailsQueryVo bankWithdrawCashDetailsQuery(BankWithdrawCashDetailsQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6073");
        json.put("FunctionFlag", dto.getFunctionFlag());
        json.put("FundSummaryAcctNo", dto.getFundSummaryAcctNo());
        json.put("SubAcctNo", dto.getSubAcctNo());
        json.put("QueryFlag", dto.getSubAcctNo());
        json.put("PageNum", dto.getPageNum());
        try {
            IBaseClient baseClient = new BankWithdrawCashDetailsQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), BankWithdrawCashDetailsQueryVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public DetailVerifiedCodeQueryVo detailVerifiedCodeQuery(DetailVerifiedCodeQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6142");
        json.put("FundSummaryAcctNo", dto.getFundSummaryAcctNo());
        json.put("OldFrontSeqNo", dto.getOldFrontSeqNo());
        json.put("OldTranType", dto.getOldTranType());

        try {
            IBaseClient baseClient = new DetailVerifiedCodeQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), DetailVerifiedCodeQueryVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public BankClearQueryVo bankClearQuery(BankClearQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6108");
        json.put("FunctionFlag", dto.getFunctionFlag());
        json.put("FundSummaryAcctNo", dto.getFundSummaryAcctNo());
        json.put("StartDate", dto.getStartDate());
        json.put("EndDate", dto.getEndDate());

        try {
            IBaseClient baseClient = new BankClearQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), BankClearQueryVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public SingleTransactionStatusQueryVo singleTransactionStatusQuery(SingleTransactionStatusQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6110");
        json.put("FunctionFlag", dto.getFunctionFlag());
        json.put("FundSummaryAcctNo", dto.getFundSummaryAcctNo());

        try {
            IBaseClient baseClient = new SingleTransactionStatusQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), SingleTransactionStatusQueryVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public CommonTransferRechargeQueryVo commonTransferRechargeQuery(CommonTransferRechargeQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6050");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("FunctionFlag", dto.getFunctionFlag());
        json.put("StartDate", dto.getStartDate());
        json.put("EndDate", dto.getEndDate());
        json.put("PageNum", dto.getPageNum());

        try {
            IBaseClient baseClient = new CommonTransferRechargeQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject obj = JSON.parseObject(httpResult.getBody());
            JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

            CommonTransferRechargeQueryVo vo = new CommonTransferRechargeQueryVo();
            if (extenddata.get(PaPayUtil.code) != null && !extenddata.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
                vo.setTxnReturnMsg(extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
                return vo;
            }
            if (extenddata.get(PaPayUtil.TXN_RETURN_CODE) != null && !extenddata.getString(PaPayUtil.TXN_RETURN_CODE).equals(PaPayUtil.code_000000)) {
                vo.setTxnReturnMsg(extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
                return vo;
            }
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), CommonTransferRechargeQueryVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }


    @Override
    public QueryCustAcctIdBalanceVo queryCustAcctIdBalance(String subAcctNo) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6093");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("SubAcctNo", subAcctNo);

        try {
            IBaseClient baseClient = new QueryCustAcctIdBalanceApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), QueryCustAcctIdBalanceVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }


    @Override
    public BankCostDsDealResultQueryVo bankCostDsDealResultQuery(BankCostDsDealResultQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6109");
        json.put("FunctionFlag", dto.getFunctionFlag());
        json.put("FundSummaryAcctNo", dto.getFundSummaryAcctNo());
        json.put("StartDate", dto.getStartDate());
        json.put("EndDate", dto.getEndDate());

        try {
            IBaseClient baseClient = new BankCostDsDealResultQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), BankCostDsDealResultQueryVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public ChargeDetailQueryVo chargeDetailQuery(ChargeDetailQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6146");
        json.put("FundSummaryAcctNo", dto.getFundSummaryAcctNo());
        json.put("AcquiringChannelType", dto.getAcquiringChannelType());
        json.put("OrderNo", dto.getOrderNo());

        try {
            IBaseClient baseClient = new ChargeDetailQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), ChargeDetailQueryVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public CustAcctIdHistoryBalanceQueryVo custAcctIdHistoryBalanceQuery(CustAcctIdHistoryBalanceQueryDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6114");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("SubAcctNo", dto.getSubAcctNo());
        json.put("StartDate", dto.getStartDate());
        json.put("EndDate", dto.getEndDate());
        json.put("PageNum", dto.getPageNum());

        try {
            IBaseClient baseClient = new CustAcctIdHistoryBalanceQueryApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject obj = JSON.parseObject(httpResult.getBody());
            JSONObject extenddata = obj.getJSONObject(PaPayUtil.extenddata) == null ? obj.getJSONObject(PaPayUtil.edata) : obj.getJSONObject(PaPayUtil.extenddata);

            CustAcctIdHistoryBalanceQueryVo vo = new CustAcctIdHistoryBalanceQueryVo();
            if (extenddata.get(PaPayUtil.code) != null && !extenddata.getString(PaPayUtil.code).equals(PaPayUtil.code_000000)) {
                vo.setTxnReturnMsg(extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
                return vo;
            }
            if (extenddata.get(PaPayUtil.TXN_RETURN_CODE) != null && !extenddata.getString(PaPayUtil.TXN_RETURN_CODE).equals(PaPayUtil.code_000000)) {
                vo.setTxnReturnMsg(extenddata.getString(PaPayUtil.TXN_RETURN_MSG));
                return vo;
            }
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), CustAcctIdHistoryBalanceQueryVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public AccountRegulationVo accountRegulation(AccountRegulationDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6145");
        json.put("FundSummaryAcctNo", dto.getFundSummaryAcctNo());
        json.put("AcquiringChannelType", dto.getAcquiringChannelType());
        json.put("OrderNo", dto.getOrderNo());
        json.put("SubAcctNo", dto.getSubAcctNo());
        json.put("TranNetMemberCode", dto.getTranNetMemberCode());
        json.put("SubAcctName", dto.getSubAcctName());
        json.put("Amt", dto.getAmt());
        json.put("Ccy", dto.getCcy());

        try {
            IBaseClient baseClient = new AccountRegulationApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), AccountRegulationVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }

    @Override
    public PlatformAccountSupplyVo platformAccountSupply(PlatformAccountSupplyDto dto) {
        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6147");
        json.put("FundSummaryAcctNo", dto.getFundSummaryAcctNo());
        json.put("AcquiringChannelType", dto.getAcquiringChannelType());
        json.put("OrderNo", dto.getOrderNo());
        json.put("Amt", dto.getAmt());

        try {
            IBaseClient baseClient = new PlatformAccountSupplyApi(sxoSafetyLogService, taskExecutePoolConfig);
            HttpResult httpResult = baseClient.execute(json);
            JSONObject extenddata = handleError(httpResult);
            return JSONObject.parseObject(JSONObject.toJSONString(extenddata), PlatformAccountSupplyVo.class);
        } catch (Exception e) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }


}
